package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import bean.Cliente;
import util.HibernateUtil;


public class ClienteDAO 
{
	private static ClienteDAO instancia = null;
	private static SessionFactory sf = null;

	public static ClienteDAO getInstancia()
	{
		if(instancia == null)
		{
			sf = HibernateUtil.getSessionFactory();
			instancia = new ClienteDAO();
		} 
		return instancia;
	}
	
	public void grabarCliente(Cliente cliente)
	{
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(cliente);
	
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void eliminarCliente( Cliente cliente )
	{
		Session session = sf.openSession();
		session.beginTransaction();
		
		session.delete( cliente );
		
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void modificarCliente( Cliente cliente )
	{
		Session session = sf.openSession();
		session.beginTransaction();
		
		session.update( cliente );
		
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	@SuppressWarnings("unchecked")
	public Cliente obtenerClientePorCUIT( String CUIT )
	{
		Session session=sf.openSession();
		
		Query query=session.createQuery("from Cliente c where c.estado = true and c.CUIT = :cuit");
		query.setParameter("cuit", CUIT);
		
		List<Cliente> cliente=query.list();
		
		session.close();
		return cliente.get(0);	
	}
}
